<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-horizon" tabindex="-1"><a class="header-anchor" href="#laravel-horizon"><span>Laravel Horizon</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#upgrading">升级 Horizon</a></li>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#configuration">配置</a></li>
<li><a href="#dashboard-authorization">仪表盘授权</a></li>
</ul>
</li>
<li><a href="#running-horizon">运行 Horizon</a>
<ul>
<li><a href="#deploying-horizon">部署 Horizon</a></li>
</ul>
</li>
<li><a href="#tags">标签</a></li>
<li><a href="#notifications">通知</a></li>
<li><a href="#metrics">Metrics</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Horizon 提供了一个漂亮的仪表盘，并且可以通过代码配置你的 Laravel Redis 队列，同时它允许你轻易的监控你的队列系统中诸如任务吞吐量，运行时间和失败任务等关键指标。.</p>
<p>所有的配置项都存放在一个简单的配置文件中，从而允许你将它放在团队的协同版本控制中，方便团队协作。</p>
<p><img src="https://cdn.learnku.com/uploads/images/201909/04/4760/wfych538yj.png!large" alt="wfych538yj.png!large"></p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<blockquote>
<p>注意：你应该确保 <code v-pre>queue</code> 配置文件中设置了 <code v-pre>redis</code> 队列驱动。</p>
</blockquote>
<p>你应该使用 Composer 来安为你的 Laravel 项目安装 Horizon：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>horizon</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>安装完成后，使用 <code v-pre>horizon:install</code> 发布 Artisan 命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan horizon<span class="token punctuation">:</span>install</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你还应该创建 <code v-pre>failed_jobs</code> 表，Laravel 将使用该表来存储任何 <a href="https://learnku.com/docs/laravel/6.x/queues#dealing-with-failed-jobs" target="_blank" rel="noopener noreferrer">failed queue jobs</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan queue<span class="token punctuation">:</span>failed<span class="token operator">-</span>table</span>
<span class="line"></span>
<span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="升级-horizon" tabindex="-1"><a class="header-anchor" href="#升级-horizon"><span>升级 Horizon</span></a></h4>
<p>当升级到 Horizon 的一个新的主要版本时，你必须仔细检查 <a href="https://github.com/laravel/horizon/blob/master/UPGRADE.md" target="_blank" rel="noopener noreferrer">the upgrade guide</a>.</p>
<p>此外，你应该重新发布 Horizon 的资源文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan horizon<span class="token punctuation">:</span>assets</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h3>
<p>发布 Horizon 相关文件后，他的主要配置文件会放在 <code v-pre>config/horizon.php</code>。你可以在这个文件中配置队列相关选项，并且每个配置项都有详细的使用说明，请详细阅读此文件。</p>
<blockquote>
<p>注意：你应确保 <code v-pre>horizon</code> 配置文件的 <code v-pre>environments</code> 部分包含你计划在其上运行 Horizon 的每个环境的条目。</p>
</blockquote>
<h4 id="均衡配置" tabindex="-1"><a class="header-anchor" href="#均衡配置"><span>均衡配置</span></a></h4>
<p>Horizon 提供了三种均衡策略： <code v-pre>simple</code> ， <code v-pre>auto</code> ，和 <code v-pre>false</code> 。默认的是 <code v-pre>simple</code> , 会将收到的任务均分给队列进程：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'balance'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'simple'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>auto</code> 策略会根据当前的工作量调整每个队列的工作进程任务数量。例如：如果 <code v-pre>notifications</code> 队列有 1000 个待执行任务，但是你的 <code v-pre>render</code> 队列是空的，Horizon 会分配更多的工作进程给 <code v-pre>notifications</code> 队列，直到 <code v-pre>notifications</code> 队列中所有任务执行完成。当配置项 <code v-pre>balance</code> 配置为 <code v-pre>false</code> ，Horizon 会使用 Laravel 默认执行行为，它将按照配置中列出的顺序处理队列任务。</p>
<p>当使用 <code v-pre>auto</code> 策略时，您可以定义 <code v-pre>minProcesses</code> 和<code v-pre>maxProcesses</code> 配置选项，以控制最小和最大进程数范围应向上和向下扩展到：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'environments'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'production'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'supervisor-1'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'connection'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'default'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'balance'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'auto'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'minProcesses'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'maxProcesses'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'tries'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="任务修整" tabindex="-1"><a class="header-anchor" href="#任务修整"><span>任务修整</span></a></h4>
<p><code v-pre>horizon</code> 配置文件允许你配置应保留最近和失败任务的时间（以分钟为单位）。 默认情况下，最近的任务保留一小时，而失败的任务保留一周：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'trim'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'recent'</span> <span class="token operator">=></span> <span class="token number">60</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'failed'</span> <span class="token operator">=></span> <span class="token number">10080</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="仪表盘权限验证" tabindex="-1"><a class="header-anchor" href="#仪表盘权限验证"><span>仪表盘权限验证</span></a></h3>
<p>Horizon 仪表盘路由是 <code v-pre>/horizon</code>. 。 默认情况下，你只能在 <code v-pre>local</code> 环境下访问仪表盘。在你的 <code v-pre>app/Providers/HorizonServiceProvider.php</code> 文件中，有一个 <code v-pre>gate</code> 方法。这里授权控制 非本地 环境中对 Horizon 的访问。 你可以根据需要随意修改此门面，以限制对 Horizon 安装的访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Register the Horizon gate.</span>
<span class="line"> *</span>
<span class="line"> * This gate determines who can access Horizon in non-local environments.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">gate</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Gate</span><span class="token operator">::</span><span class="token function">define</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'viewHorizon'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">in_array</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：记住，Laravel会自动将 <em>authenticated</em> 用户注入到 gate 方法。如果您的应用程序通过其他方法（如IP限制）提供 Horizon 安全，那么 Horizon 用户可能不需要“登录”。因此，您需要将上面的 <code v-pre>function ($user)</code> 更改为 <code v-pre>function ($user = null)</code>以使 Laravel 不需要进行身份验证。</p>
</blockquote>
<h2 id="运行-horizon" tabindex="-1"><a class="header-anchor" href="#运行-horizon"><span>运行 Horizon</span></a></h2>
<p>当在 <code v-pre>config/horizon.php</code> 文件中配置好了你的队列执行进程后，你就可以使用 <code v-pre>horizon</code> Artisan 命令启动 Horizon。只需要一条命令语句即可启动所有配置好的队列进程：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan horizon</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以使用 <code v-pre>horizon:pause</code> 和 <code v-pre>horizon:continue</code> Artisan 命令来暂停或继续执行队列任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan horizon<span class="token punctuation">:</span>pause</span>
<span class="line"></span>
<span class="line"><span class="token package">php</span> artisan horizon<span class="token punctuation">:</span><span class="token keyword">continue</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>horizon:terminate</code> Artisan 命令优雅的终止 Horizon 主进程。Horizon 会把正在执行的任务处理完毕后退出：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan horizon<span class="token punctuation">:</span>terminate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="部署-horizon" tabindex="-1"><a class="header-anchor" href="#部署-horizon"><span>部署 Horizon</span></a></h3>
<p>如果你将 Horizon 部署到线上服务器时，则需要配置一个进程监控器来检测 <code v-pre>php artisan horizon</code> 命令，在它意外退出时自动重启。上线新代码时则需要该进程监控器终止 Horizon 进程并以修改后的代码重启 Horizon。</p>
<h4 id="supervisor-配置" tabindex="-1"><a class="header-anchor" href="#supervisor-配置"><span>Supervisor 配置</span></a></h4>
<p>如果你使用 Supervisor 进程监控器管理你的 <code v-pre>horizon</code> 进程，那么以下配置文件则可满足需求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span>program<span class="token punctuation">:</span>horizon<span class="token punctuation">]</span></span>
<span class="line">process_name<span class="token operator">=</span><span class="token operator">%</span><span class="token punctuation">(</span>program_name<span class="token punctuation">)</span>s</span>
<span class="line">command<span class="token operator">=</span>php <span class="token operator">/</span>home<span class="token operator">/</span>forge<span class="token operator">/</span>app<span class="token operator">.</span>com<span class="token operator">/</span>artisan horizon</span>
<span class="line">autostart<span class="token operator">=</span><span class="token constant boolean">true</span></span>
<span class="line">autorestart<span class="token operator">=</span><span class="token constant boolean">true</span></span>
<span class="line">user<span class="token operator">=</span>forge</span>
<span class="line">redirect_stderr<span class="token operator">=</span><span class="token constant boolean">true</span></span>
<span class="line">stdout_logfile<span class="token operator">=</span><span class="token operator">/</span>home<span class="token operator">/</span>forge<span class="token operator">/</span>app<span class="token operator">.</span>com<span class="token operator">/</span>horizon<span class="token operator">.</span>log</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>Tip：如果你不喜欢自己维护服务器，可以考虑使用 <a href="https://forge.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Forge</a> 。Forge 提供了运行一个带有 Horizon 的现代、强大的 Laravel 应用所需要的 PHP7+ 及其他所有环境。</p>
</blockquote>
<h2 id="标签" tabindex="-1"><a class="header-anchor" href="#标签"><span>标签</span></a></h2>
<p>Horizon 允许你对任务分配「标签」，包括邮件，事件广播，通知和队列的事件监听器。事实上，Horizon 会智能并且自动根据任务携带 Eloquent 模型给大多数任务标记标签，如下任务示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Jobs</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Video</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>SerializesModels</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>InteractsWithQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Dispatchable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">RenderVideo</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Dispatchable</span><span class="token punctuation">,</span> InteractsWithQueue<span class="token punctuation">,</span> Queueable<span class="token punctuation">,</span> SerializesModels<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * video 实例</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token punctuation">\</span>App<span class="token punctuation">\</span>Video</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$video</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建工作实例</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token punctuation">\</span>App<span class="token punctuation">\</span>Video</span>  <span class="token parameter">$video</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Video</span> <span class="token variable">$video</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">video</span> <span class="token operator">=</span> <span class="token variable">$video</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 执行任务</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果该队列任务是一个携带 <code v-pre>id</code> 为 <code v-pre>1</code> 的 <code v-pre>App\Video</code> 实例，那么它将自动被标记上 <code v-pre>App\Video:1</code> 标签。因为 Horizon 会检查任务属性是否具有 Eloquent 模型，如果发现 Eloquent 模型，Horizon 将会智能的用该模型的类名和主键为任务标记上标签：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$video</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Video</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>RenderVideo</span><span class="token operator">::</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token variable">$video</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义标签" tabindex="-1"><a class="header-anchor" href="#自定义标签"><span>自定义标签</span></a></h4>
<p>如果你想手动的为队列执行的对象定义标签，你可以给这个类定义一个 <code v-pre>tags</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">RenderVideo</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取分配给这个任务的标签</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">tags</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'render'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'video:'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">video</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="通知" tabindex="-1"><a class="header-anchor" href="#通知"><span>通知</span></a></h2>
<blockquote>
<p><strong>Note:</strong> 配置Horizon以发送Slack或SMS通知时，应查看 <a href="https://learnku.com/docs/laravel/6.x/notifications" target="_blank" rel="noopener noreferrer">相关通知驱动程序的前提条件</a>.</p>
</blockquote>
<p>如果需要在队列等待时间过长时发起通知，可以在应用的 <code v-pre>HorizonServiceProvider</code> 中调用 <code v-pre>Horizon::routeMailNotificationsTo</code>, <code v-pre>Horizon::routeSlackNotificationsTo</code> , 和 <code v-pre>Horizon::routeSmsNotificationsTo</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Horizon</span><span class="token operator">::</span><span class="token function">routeMailNotificationsTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'example@example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Horizon</span><span class="token operator">::</span><span class="token function">routeSlackNotificationsTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'slack-webhook-url'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'#channel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Horizon</span><span class="token operator">::</span><span class="token function">routeSmsNotificationsTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'15556667777'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="配置等待时间过长的阈值" tabindex="-1"><a class="header-anchor" href="#配置等待时间过长的阈值"><span>配置等待时间过长的阈值</span></a></h4>
<p>你可以在 <code v-pre>config/horizon.php</code> 配置文件中设置等待时间过长的具体秒数。 <code v-pre>waits</code> 配置项可以针对每一个 链接 / 队列 配置阈值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'waits'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'redis:default'</span> <span class="token operator">=></span> <span class="token number">60</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="metrics" tabindex="-1"><a class="header-anchor" href="#metrics"><span>Metrics</span></a></h2>
<p>Horizon 包含一个 Metrics 仪表盘，它可以提供任务和队列等待时间和吞吐量信息，为了填充此仪表盘，你需要通过应用的<a href="https://learnku.com/docs/laravel/6.x/scheduling" target="_blank" rel="noopener noreferrer">任务调度器</a>配置Horizon 的 <code v-pre>snapshot</code> Artisan命令每五分钟运行一次。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 定义应用程序的任务调度</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Console<span class="token punctuation">\</span>Scheduling<span class="token punctuation">\</span>Schedule</span>  <span class="token parameter">$schedule</span></span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">schedule</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Schedule</span> <span class="token variable">$schedule</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$schedule</span><span class="token operator">-></span><span class="token function">command</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'horizon:snapshot'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">everyFiveMinutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/horizon/5190" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/hor...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/horizon/5190" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/hor...</a></p>
</blockquote>
</div></template>


